"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.onNormFile = void 0;

require("antd/lib/upload/style/css");

var _upload = _interopRequireDefault(require("antd/lib/upload"));

require("antd/lib/button/style/css");

var _button = _interopRequireDefault(require("antd/lib/button"));

require("antd/lib/message/style/css");

var _message2 = _interopRequireDefault(require("antd/lib/message"));

require("antd/lib/form/style/css");

var _form = _interopRequireDefault(require("antd/lib/form"));

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _icons = require("@ant-design/icons");

var _util = require("./util");

var _localStorage = _interopRequireDefault(require("../utils/localStorage"));

var _configure = require("../utils/configure");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

// 获取默认配置
var FormItem = _form.default.Item; // 上传文件

/*
 * 输出到外面的数据结构
 * */

var onNormFile = function onNormFile(uploadingFilesNow, e) {
  var fileList = e.fileList;

  if (Array.isArray(e)) {
    return e;
  } // 并发操作处理


  fileList.forEach(function (res, index) {
    if (!(0, _util.onBeforeUpload)(res, fileList, 1)) {
      uploadingFilesNow(false);
      fileList.splice(index, 1);
    }

    if (fileList.length > 50) {
      uploadingFilesNow(false);

      _message2.default.error('最多只能上传50个文件!');

      fileList.splice(50, fileList.length - 1);
    }

    if (res.status === 'done') {
      uploadingFilesNow(true);

      if (res.response && res.response.success !== 1) {
        _message2.default.error(res.response.message);

        fileList.splice(index, 1);
      }

      if (res.response && res.response.success === 1) {
        // 上传成功
        // todo 新项目，这里可能要改（因为数据结构可能不一样）
        _message2.default.success(res.response.message);

        fileList.splice(index, 1, {
          uid: res.uid,
          name: res.name,
          fileName: res.name,
          fileCode: res.response.models[0].code,
          fileId: res.response.models[0].id
        });
      }

      if (res.response && res.response.success === 0 && res.response.resultCode === '00999') {
        setTimeout(function () {// getDvaApp()._store.dispatch({ type: 'login/logout' });
        }, 800);
      }
    }
  });
  return fileList;
};

exports.onNormFile = onNormFile;

function Index(props) {
  var _props$uploadingFiles = props.uploadingFilesNow,
      uploadingFilesNow = _props$uploadingFiles === void 0 ? function () {} : _props$uploadingFiles;
  return /*#__PURE__*/_react.default.createElement(FormItem, _extends({}, props, {
    valuePropName: "fileList",
    getValueFromEvent: function getValueFromEvent(e) {
      return onNormFile(uploadingFilesNow, e);
    },
    extra: "\u652F\u6301\u6269\u5C55\u540D\uFF1A.rar .zip .doc .docx .pdf .jpg"
  }), /*#__PURE__*/_react.default.createElement(_upload.default, {
    listType: "text",
    disabled: props.disabled,
    action: (0, _configure.getConfigure)().uploadApi,
    headers: {
      Client: (0, _configure.getConfigure)().client,
      Authorization: _localStorage.default.getItem('token')
    },
    beforeUpload: _util.onBeforeUpload,
    onPreview: _util.preViewFile
  }, /*#__PURE__*/_react.default.createElement(_button.default, {
    disabled: props.disabled
  }, /*#__PURE__*/_react.default.createElement(_icons.UploadOutlined, null), " \u4E0A\u4F20\u6587\u4EF6")));
}
/*
 * 其他参数 和FormItem组件一致
 *
 * */


Index.propTypes = {
  label: _propTypes.default.string,
  name: _propTypes.default.string
};

var _default = _react.default.memo(Index);

exports.default = _default;